package com.wxn.learncompose.pagers

import android.widget.Toast
import androidx.compose.animation.animateContentSize
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import com.wxn.learncompose.MyApp
import com.wxn.learncompose.R
import com.wxn.learncompose.beans.Message
import com.wxn.learncompose.datas.SampleData

@Composable
fun MainPager() {
    Surface(
        modifier = Modifier.fillMaxSize(),
        color = MaterialTheme.colorScheme.background) {
        Column {
            Conversation()
            Box(modifier = Modifier.size(width = 120.dp, height = 60.dp)
                .background(Color(0xFF0099EE))
                .padding(all = 1.dp)) {
                Button(
                    onClick = { Toast.makeText(MyApp.context(), "onClick", Toast.LENGTH_SHORT).show() }
                ) {
                    Text(text = "Next")
                }
            }
        }
    }
}

@Composable
fun Conversation() {
    val conversations by remember {
        mutableStateOf(SampleData.messages)
    }
    LazyColumn {
        items(conversations.size) {
            MessageCard(conversations[it])
        }
    }
}

@Composable
fun MessageCard(message: Message) {
    var isExpanded by remember {
        mutableStateOf(false)
    }
    var surfaceColor = if (isExpanded) Color(0xFF99EF33) else Color.Transparent

    Surface(
        shape = MaterialTheme.shapes.medium,
        shadowElevation = 5.dp,
        modifier = Modifier
            .padding(horizontal = 8.dp, vertical = 4.dp)
            .fillMaxWidth(1.0f)
    ) {
        Row(
            modifier = Modifier.padding(all = 8.dp)
        ) {
            Image(
                painterResource(id = R.drawable.ic_launcher_background),
                contentDescription = "head",
                modifier = Modifier
                    .size(56.dp)
                    .clip(CircleShape)
                    .border(
                        width = 1.5.dp, MaterialTheme.colorScheme.primary, RoundedCornerShape(4.dp)
                    )
            )
            Spacer(modifier = Modifier.padding(horizontal = 8.dp))
            Column(
                modifier = Modifier
                    .clickable {
                        isExpanded = !isExpanded
                    }
                    .animateContentSize { initialValue, targetValue -> }
            ) {
                Text(text = message.author,
                    color = MaterialTheme.colorScheme.primary,
                    style = MaterialTheme.typography.titleSmall
                    )
                Spacer(modifier = Modifier.padding(vertical = 4.dp))
                Surface(
                    color = surfaceColor,
//                    modifier = Modifier.border(width = 1.dp, color = Color.LightGray, RoundedCornerShape(0.5.dp))
                ) {
                    Text(text = message.msg,
                        color = MaterialTheme.colorScheme.secondary,
                        maxLines = if (isExpanded) Int.MAX_VALUE else 1,
                        style = MaterialTheme.typography.bodySmall)
                }
            }
        }
    }
}
